// 这个数据，是用于：添加（修改）管理员时，设定权限
// 1、定义全部的菜单数据
export const menudata = [
    {
        id: "1",
        label: "主页",
        path: "/admin/homePage"
    },
    {
        id: "2",
        label: "商品",
        children: [
            {
                id: "2-1",
                label: "查看商品",
                path: "/admin/addGoods"
            },
            {
                id: "2-2",
                label: "商品列表",
                path: "/admin/shopLists"
            }
        ]
    },
    {
        id: "3",
        label: "轮播图管理",
        children: [
            {
                id: "3-1",
                label: "轮播图列表",
                path: "/admin/imgShow",
                children:[
                    {
                        id:"3-1-R",
                        label:"轮播图列表显示"
                    },
                    {
                        id:"3-1-U",
                        label:"是否启用"
                    },
                    {
                        id:"3-1-D",
                        label:"是否删除"
                    }
                ]
            },
            {
                id: "3-2",
                label: "轮播图添加",
                path: "/admin/addImg"
            }
        ]
    },
    {
        id: "4",
        label: "管理人员",
        children: [
            {
                id: "4-1",
                label: "管理人员列表",
                path: "/admin/manageList",
                children:[
                    {
                        id:"4-1-U",
                        label:"修改管理员数据"
                    },{
                        id:"4-1-D",
                        label:"删除管理员"
                    }
                ]
            },
            {
                id: "4-2",
                label: "添加管理人员",
                path: "/admin/addManage"
            }
        ]
    },
    {
        id: "5",
        label: "订单管理",
        children: [
            {
                id: "5-1",
                label: "查看订单",
                path: "/admin/showOrder",
                children:[
                    {
                        id:"5-1-U",
                        label:"是否修改订单状态"
                    },
                    {
                        id:"3-1-R",
                        label:"订单列表显示"
                    },
                ]
            }
        ]
    },
];

// 2、根据当前用户的权限，产生相应的菜单

export default function getMenus () {
    //  1）、获取该用户的权限（登录时已经保存了）
    let checkedKeys:string[] = [];// ['1','3','3-1']
    let currKeys = sessionStorage.getItem("grants");
    if (currKeys) {
        checkedKeys = JSON.parse(currKeys)
    }

    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1'];
    }

    // 2）、根据该用户的权限结合全部菜单数据，产生该用户的菜单

    interface IMenuItem{
        id:string,
        label:string,
        path?:string,
        children?:IMenuItem[]
    }

    let currMenuData:IMenuItem[] = [];//存储当前用户的菜单项

    menudata.forEach(item=>{
        if(checkedKeys.indexOf(item.id)>-1){
            //定义一个菜单项：            
            const newmenuItem = {...item};  
            // 处理子一级
            if(item.children){
                newmenuItem.children = [];
                item.children.forEach(sonItem=>{
                    if(checkedKeys.indexOf(sonItem.id)>-1){
                        newmenuItem.children?.push({...sonItem});
                    }
                })
            }
            currMenuData.push(newmenuItem);
        }
    });

    console.log("currMenuData",currMenuData);
    
    return currMenuData;
    
}